package edu.neu.swc.skynet.controller;

import edu.neu.swc.skynet.movie.MovieInfo;
import edu.neu.swc.skynet.service.async.AsyncTaskManager;
import edu.neu.swc.skynet.vo.AsyncTaskInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <h1>异步电影HTTP接口</h1>
 * 异步任务对外提供HTTP接口
 * RESTful风格
 */
@RestController
@Slf4j
@Api(tags = "电影信息异步任务")
@RequestMapping("/async-movies")
public class AsyncMovieController {

    private final AsyncTaskManager asyncTaskManager;

    public AsyncMovieController(AsyncTaskManager asyncTaskManager) {
        this.asyncTaskManager = asyncTaskManager;
    }

    @ApiOperation(value = "导入电影",
            notes = "异步导入电影列表信息",
            httpMethod = "POST"
    )
    @PostMapping("/import-movie")
    public AsyncTaskInfo importMovies(@RequestBody
                                              List<MovieInfo> movieInfos){
        return asyncTaskManager.submit(movieInfos);
    }

    @ApiOperation(value = "查询导入情况",
            notes = "导入电影信息",
            httpMethod = "GET"
    )
    @GetMapping("/task-info")
    public AsyncTaskInfo getTaskInfo(@RequestParam String taskId){
        return asyncTaskManager.getTaskInfo(taskId);
    }

    @ApiOperation(value = "同步电影",
            notes = "异步同步数据库和搜索引擎",
            httpMethod = "GET"
    )
    @GetMapping("/sync-movie")
    public AsyncTaskInfo syncMovieShow(){
        return asyncTaskManager.sync();
    }
}
